import BaseEvent from '@/components/events/BaseEvent'
import { isWeibo, getRandomArrayElements, isJSON, getCompIdByParam } from '@/plugins/utils'
import { FRONT_EVENT_TYPE_MAPS } from '@/common/events'
import { EventBus } from '@/store/index'
import { MOBILE_WID_HEI } from '@/common/const-dif'
import { COMPONENT_TYPES, CONTROL_TYPES } from '@/common/const-wb'
import { isHasOwnProperty } from '@/plugins/utils'

export default class InteractionEvent extends BaseEvent{
  constructor() {
    super();
  }

  excute({item, event, clickItem, isPostWbEventOrCommonEvents}){
    this.item = item;
    this.event = event;

    const frontEventType = event.comps[0].attrs.value;
    let isContinue = true;
    if(frontEventType == FRONT_EVENT_TYPE_MAPS.swiperPre){
      EventBus.$emit("swiperPre", { item: this.item });
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.swiperNext){
      EventBus.$emit("swiperNext", { item: this.item });
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.playSpriteSheetAnim){
      EventBus.$emit("playGroupSpriteSheetAnim", { item: this.item });
      EventBus.$emit("playSpriteSheetAnim");
      isContinue = false;
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.removeSpriteSheetText){
      EventBus.$emit("removeSpriteSheetText");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.confirmSpriteSheetText){
      const paramStr = event.comps[1].attrs.value;
      // {"relateCompId":"85311510-2382-4feb-8c4a-e9ff747553e7","pageId":4}
      if(paramStr && isJSON(paramStr)){
        const paramObj = JSON.parse(paramStr);
        EventBus.$emit("confirmSpriteSheetText", paramObj);
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.textChangeAddHiddenId){
      const compIdStr = event.comps[1].attrs.value;
      const compIdObj = JSON.parse(compIdStr);
      const hiddenCompId = compIdObj.hiddenCompId;
      const saveImgCompId = compIdObj.saveImgCompId;
      const saveImgCompData = this.componentMap[saveImgCompId];
      let saveImgEvent = _.cloneDeep(saveImgCompData.events.saveImg);
      const hiddenCompIdStr = saveImgEvent.comps[1].attrs.hiddenCompIds;
      const hiddenCompIds = JSON.parse(hiddenCompIdStr);
      _.pull(hiddenCompIds, hiddenCompId);
      saveImgEvent.comps[1].attrs.hiddenCompIds = JSON.stringify(hiddenCompIds);
      this.store.commit("updateComponentAttr", { id: saveImgCompId, events: {saveImg: saveImgEvent} })
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.swiper){
      let interactionData_ = this.interactionData;
      const relateCompId = getCompIdByParam(event.comps[1].attrs.relateCompId);
      const relateCompData = this.componentMap[relateCompId];
      const clickDataStr = event.comps[1].attrs.value;
      let isBackground = '';
      if(relateCompId){
        if(clickDataStr && isJSON(clickDataStr)){
          let clickData = JSON.parse(clickDataStr);
          isBackground = clickData.isBackground || clickData.imgRelateCompId;
        }
        //兼容之前的可以将数据存放在event_.comps[1].attrs.imgs里，也可以存放到item.interactionData里
        let imgs = [];
        if(item.interactionData){
          if(item.interactionData.isDimension && item.interactionData.dimensionLists){
            imgs = item.interactionData.dimensionLists
          }else{
            imgs = item.interactionData.lists
          }
        }
        let swiperList;
        if(interactionData_.swiperIndex >= 0 && _.isArray(imgs[0])){
          swiperList = imgs[interactionData_.swiperIndex];
        }else{
          swiperList = imgs;
        }
        let lists = [];
        _.forEach(swiperList, (item) => {
          lists.push({ url: item.url, link: "" })
        })
        lists.push({ url: '', link: "" });
        this.store.commit("replaceComponentAttr", { id: relateCompId, lists })
        //isBackground这个属性是想背景这样的，直接替换组件url，其他的都是添加到滚动容器里的
        this.store.commit("updateComponentAttr", { id: relateCompId, commonAttr: { isBackground } })
      }
      //交互事件更新幻灯片组件数据
      EventBus.$emit("swiperChangePage");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.urlToCanvas){
      EventBus.$emit("addImgToCanvas", { url: item.commonAttr.url, width: item.conAttr.width/MOBILE_WID_HEI.adaptiveScale, height: item.conAttr.height/MOBILE_WID_HEI.adaptiveScale });
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.randomOpenPage){
      let interactionData_ = this.interactionData;
      //823 祖玛珑寻找威廉梨人 1791 奶爸请就位 按钮触发下一题
      const pageDataStr = event.comps[1].attrs.value;
      let randomPageIds = [];
      let randomPageIndex = 0;
      let pageId = -1;
      let popId = -1;
      let interactionData = {};
      if(pageDataStr && pageDataStr.length > 0){
        const pageData = JSON.parse(pageDataStr);
        //{"randomArr":[2,3,4,5,6],"notSameArr":[4,6],"randomCount":3,"lastPageId":7,"time":2}
        if(isHasOwnProperty(pageData, "notSameArr")) {
          randomPageIds = getRandomArrayElements(pageData.randomArr,pageData.notSameArr,pageData.randomCount)
        }else if(isHasOwnProperty(pageData, "randomArr")) {
          randomPageIds = pageData.randomArr;
        }
        randomPageIndex = 0;
        const relateCompId = getCompIdByParam(event.comps[1].attrs.relateCompId);
        if(relateCompId){
          const rules = JSON.parse(event.comps[1].attrs.rules);
          interactionData = { randomPageIds, randomPageIndex: randomPageIndex + 1, relateCompId, rules };
        }else{
          interactionData = { randomPageIds, randomPageIndex: randomPageIndex + 1 };
        }
        _.merge(interactionData, pageData);
        interactionData_ = interactionData;
      }else{
        randomPageIds = interactionData_.randomPageIds;
        randomPageIndex = interactionData_.randomPageIndex
        interactionData_.randomPageIndex = interactionData_.randomPageIndex + 1;
      }

      //更新下一次交互的数据和结果页面 1820打组 自定义数据
      if(randomPageIds.length == 0 && randomPageIndex < interactionData_.isRights.length || randomPageIndex < randomPageIds.length) {
        this.store.commit("updateInteractionData", interactionData);
        if(randomPageIds.length == 0) {
          EventBus.$emit("swiperNext", { item: this.item });
        } else {
          pageId = randomPageIds[randomPageIndex];
          if(pageId && pageId > 0) {
            window.setTimeout(() => {
              this.showPage(pageId);
            }, interactionData_.time*1000)
          }
        }
      }else {
        if(isHasOwnProperty(interactionData_, "lastPageId")) {
          pageId = interactionData_.lastPageId;
        }
        if(isHasOwnProperty(interactionData_, "lastPopId")) {
          popId = interactionData_.lastPopId;
        }
        //默认规则
        if(interactionData_.rules && isHasOwnProperty(interactionData_.rules, "pushIndexScores")) {
          this.scoreRules(interactionData_);
        } else {
          this.combinationRules(interactionData_);
        }
        if(pageId > 0 || popId > 0) {
          window.setTimeout(() => {
            if(pageId > 0) {
              this.showPage(pageId);
            }
            if(popId > 0) {
              this.showPop(popId);
            }
            this.store.commit("resetInteractionData");
          }, interactionData_.time*1000)
        }
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.weiboWeixin){
      const weixinCompId = getCompIdByParam(event.comps[1].attrs.weixinCompId);
      const weiboCompId = getCompIdByParam(event.comps[1].attrs.weiboCompId);
      if(weixinCompId && weixinCompId.length > 0){
        if(isWeibo()){
          this.store.commit("updateComponentAttr", {id: weixinCompId, commonAttr: {isVisible: false}})
        }else{
          this.store.commit("updateComponentAttr", {id: weiboCompId, commonAttr: {isVisible: false}})
        }
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.videoMute){
      EventBus.$emit("videoMute");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.videoPlayAudio){
      EventBus.$emit("videoPlayAudio");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.playVideo){
      const videoStr = event.comps[1].attrs.value;
      if(videoStr && isJSON(videoStr)){
        const videoData = JSON.parse(videoStr);
        EventBus.$emit("playVideo", videoData);
      }else{
        EventBus.$emit("playVideo");
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.switchVideo){
      const videoStr = event.comps[1].attrs.value;
      if(videoStr && isJSON(videoStr)){
        const videoData = JSON.parse(videoStr);//{url:"",timeSectionList:[{time:2,popId:1,pageId:1,compId:""}]}
        EventBus.$emit("switchVideo", videoData);
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.playAudio){
      // 图片和按钮组件中会 设置这个值
      let isPlay = this.item.commonAttr.isSelected_
      EventBus.$emit("playOrPauseAudio", { isPlay, isTouch: true });
      EventBus.$emit("playVideo");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.pauseAudio){
      EventBus.$emit("playOrPauseAudio", { isPlay: false });
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.changeRelateCompData){
      this.changeRelateCompData(event, clickItem);
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.openCamera){
      EventBus.$emit("openCamera");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.rotateCameraImg){
      EventBus.$emit("rotateCameraImg");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.removeCamera){
      EventBus.$emit("removeCamera");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.slotMachine){
      EventBus.$emit("slotMachine");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.graspDoll){
      EventBus.$emit("graspDoll");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.exportSVG){
      EventBus.$emit("exportSVG");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.copyText){
      let textCompIdStr = event.comps[1].attrs.value;
      if(textCompIdStr && isJSON(textCompIdStr)) {
        let textCompObj = JSON.parse(textCompIdStr);
        let textCompId = textCompObj.textCompId;
        if(isHasOwnProperty(item, 'isInGroupCarousel') && item.isInGroupCarousel) {
          const textCompData = this.componentMap[textCompId];
          let idIndexs = item.id.split('_');
          textCompId = textCompId + '_' + idIndexs[1] + '_' + textCompData.commonAttr.qIndex;
        }
        this.clipboardCopy(this.formValues[textCompId].value);
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.randomText) {
      let event = this.item.events.interactionData;
      if(event) {
        let relateCompId = getCompIdByParam(event.comps[1].attrs.value);
        let lists = this.item.interactionData.lists;
        let length = lists.length;
        let randomIndex = _.random(0, length-1);
        let item = lists[randomIndex];
        if(item && item.params && isJSON(item.params)){
          let text = JSON.parse(item.params).text;
          this.store.commit("updateComponentAttr", { id: relateCompId, commonAttr: {text} });
          EventBus.$emit("refreshDynamicData")
        }
      }
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.twistedEgg) {
      EventBus.$emit("twistedEgg", isPostWbEventOrCommonEvents);
      isContinue = false;
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.passData) {
      EventBus.$emit("passData", {itemIndex: this.item.commonAttr.itemIndex});
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.turnBook) {
      EventBus.$emit("turnBook");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.startTimer) {
      EventBus.$emit("timerStart");
    }else if(frontEventType == FRONT_EVENT_TYPE_MAPS.clearPaper) {
      EventBus.$emit("clearPaper");
    }else if(frontEventType === FRONT_EVENT_TYPE_MAPS.switchAudio){
      let relateAttrCompId = getCompIdByParam(this.item.commonAttr.relateAttrCompId)
      if(relateAttrCompId){
        let itemIndex =  this.componentMap[relateAttrCompId].commonAttr.itemIndex
        let list = this.item.interactionData.lists[itemIndex]
        _.merge(list, JSON.parse(list.params))
        EventBus.$emit('switchAudio', {
          url: list.url,
          name: list.name
        })
      }
    }else if(frontEventType === FRONT_EVENT_TYPE_MAPS.switchScroll){
      // 切换横向滚动容器
      let compStr = event.comps[1].attrs.value
      if (compStr) {
        let compObj = JSON.parse(compStr)
        if (compObj.relateCompId) {
          let url = item.lists[item.commonAttr.itemIndex].url
          this.store.commit("updateComponentAttr", { id: compObj.relateCompId, commonAttr: {url} });
        }
      }
    }
    return isContinue;
  }

  copyClipboardApi (text) {
    // Use the Async Clipboard API when available. Requires a secure browsing
    // context (i.e. HTTPS)
    if (!navigator.clipboard) {
      throw this.makeError()
    }
    return navigator.clipboard.writeText(text)
  }

  makeError () {
    return new DOMException('The request is not allowed', 'NotAllowedError')
  }

  copyExecCommand (text) {
    // Put the text to copy into a <span>
    const span = document.createElement('span')
    span.textContent = text

    // Preserve consecutive spaces and newlines
    span.style.whiteSpace = 'pre'
    span.style.webkitUserSelect = 'auto'
    span.style.userSelect = 'all'

    // Add the <span> to the page
    document.body.appendChild(span)

    // Make a selection object representing the range of text selected by the user
    const selection = window.getSelection()
    const range = window.document.createRange()
    selection.removeAllRanges()
    range.selectNode(span)
    selection.addRange(range)

    // Copy text to the clipboard
    let success = false
    try {
      success = window.document.execCommand('copy')
    } finally {
      // Cleanup
      selection.removeAllRanges()
      window.document.body.removeChild(span)
    }

    if (!success) throw this.makeError()
  }

  async clipboardCopy (text) {
    let isSuccess = false
    try {
      isSuccess = await this.copyClipboardApi(text)
    } catch (err) {
      try {
        isSuccess = await this.copyExecCommand(text)
      } catch (err2) {
        throw (err2 || err || this.makeError())
      }
    }
    if(isSuccess) {
      if(textCompObj.hint){
        this.showToast(textCompObj.hint)
      }else{
        this.showToast("复制失败！")
      }
    } else {
      this.showToast("复制成功！")
    }
  }

  scoreRules(interactionData_) {
    const clickCompIdMap = interactionData_.clickCompIdMap;

    const clickScores = [];
    //通过选项对应的分数规则 返回发博索引{"rules":{"1":10,"2":20,"3":"30","4":"40"},"pushIndexScores":[40,90,120,160]}
    let rules = interactionData_.rules.rules;//{"1":10,"2":20,"3":"30","4":"40"}
    let clickTotalScore = 0;
    let score = 0;
    let isRights = [];
    // interactionData_.isRights = [5,2,3];//测试数据
    let isHaveRights = isHasOwnProperty(interactionData_, 'isRights');
    let index = 0;
    for (let item of clickCompIdMap.values()) {
      score =  parseInt(rules[item.relateType]);
      clickTotalScore += score;
      clickScores.push(score);
      if(isHaveRights) {
        if(interactionData_.isRights[index] == parseInt(item.relateType)) {
          isRights.push(true);
        } else {
          isRights.push(false);
        }
      }
      index++;
    }
    let searchIndex = 0;
    let pushIndexScores = interactionData_.rules.pushIndexScores;
    for(let i = 0, len = pushIndexScores.length; i < len; i++) {
      if(clickTotalScore >= pushIndexScores[i]) {
        searchIndex = i;
      }
    }
    //更新bindData 全局分数 clickTotalScore
    this.store.commit('updateBindData', {key: 'score', value: clickTotalScore});
    this.store.commit('updateBindData', {key: 'scores', value: clickScores});
    if(isHaveRights) {
      this.store.commit('updateBindData', {key: 'isRights', value: isRights});
    }
    if(isHasOwnProperty(interactionData_, 'imgRelateCompIds') && interactionData_.imgRelateCompIds) {
      const imgRelateCompIds = interactionData_.imgRelateCompIds;
      const imgCompIds = imgRelateCompIds.split(",");
      _.forEach(imgCompIds, (imgRelateCompId) => {
        const imgRelateCompData = this.componentMap[imgRelateCompId];
        const imgUrl = imgRelateCompData.lists[searchIndex].url;
        this.store.commit("updateComponentAttr", { id: imgRelateCompId, commonAttr: {url: imgUrl, itemIndex: searchIndex} });
      })
    }
    if(isHasOwnProperty(interactionData_, 'pushCompId') && interactionData_.pushCompId) {
      const pushCompId = interactionData_.pushCompId;
      const pushCompData = this.componentMap[pushCompId];
      pushCompData.commonAttr.pushIndex = searchIndex;
    }
    //更新计数每次增加数据 2152
    if(isHasOwnProperty(interactionData_, 'numberCompId') && interactionData_.numberCompId) {
      let numberControlData = this.controls[interactionData_.numberCompId][CONTROL_TYPES.number];
      numberControlData.data.onceGrowValue = numberControlData.data.vipOnceGrowValue = clickTotalScore;
      // this.store.commit('setWbNumber', {id: interactionData_.numberCompId, controlId: numberControlData.controlId, data: numberControlData.data})
    }
  }

  mapRules(rules,result,pushIndexMap){
    // 站点2337
    //{"rules":[[1,5],[1,6],[1,7],[1,8],[2,5],[2,6],[3,7],[4,8],[3,5],[3,6],[3,7],[3,8],[4,5],[4,6],[4,7],[4,8]],"pushIndexMap":{"0":0,"1":0,"2":1,"3":3,"4":3,"5":0,"6":1,"7":3,"8":0,"9":1,"10":2,"11":2,"12":3,"13":2,"14":1,"15":3}}
    let searchIndex = 0
    for (let index = 0; index < rules.length; index++) {
      if(rules[index].toString() === result.toString()){
        searchIndex = pushIndexMap[index]
        break
      }
    }
    return searchIndex
  }

  combinationRules(interactionData_) {
    const clickCompIdMap = interactionData_.clickCompIdMap;
    const interactionRules = interactionData_.rules;
    const interactionResultImgs = interactionData_.imgs;
    let clickRuleTypes = [];
    let searchImgIndex = 0;
    const relateCompId = interactionData_.relateCompId;
    const imgRelateCompIds = interactionData_.imgRelateCompIds;
    const bgIndexs = interactionData_.bgIndexs;
    const bgRelateCompId = interactionData_.bgRelateCompId;

    let relateCompList = [];
    let index = 0;
    for (let item of clickCompIdMap.values()) {
      clickRuleTypes.push(parseInt(item.relateType));
      relateCompList[index] = { url: item.relateUrl };
      index++;
    }
    //更新关联组件属性，通用列表图片组
    if(relateCompId){
      this.store.commit("updateComponentAttr", { id: relateCompId, lists: relateCompList });
    }
    if(interactionRules && isHasOwnProperty(interactionRules, "pushIndexMap")){
      // 有顺序规则 站点 2337
      const pushIndexMap = interactionRules.pushIndexMap
      searchImgIndex =  this.mapRules(interactionRules.rules, clickRuleTypes, pushIndexMap)
    }else{
      // 站点 823
      // 规则 [[1,1,1],[1,3,3],[1,2,3],[1,1,2],[1,2,2],[1,1,3],[2,2,3],[3,3,2],[3,3,3],[2,2,2]]
      for(let i = 0, len = interactionRules.length; i < len; i++){
        let rules = _.cloneDeep(interactionRules[i]);
        let length = 0;
        let ruleIndex = -1;
        _.forEach(clickRuleTypes, (value) => {
          ruleIndex = rules.indexOf(value);
          if(ruleIndex != -1){
            _.pullAt(rules, ruleIndex);
            length++;
          }
        })
        if(length == clickRuleTypes.length){
          searchImgIndex = i;
          break;
        }
      }
    }
    //更新关联图片组件属性
    if(imgRelateCompIds){
      const imgCompIds = imgRelateCompIds.split(",");
      _.forEach(imgCompIds, (imgRelateCompId) => {
        const imgRelateCompData = this.componentMap[imgRelateCompId];
        const imgUrl = imgRelateCompData.lists[searchImgIndex].url;
        this.store.commit("updateComponentAttr", { id: imgRelateCompId, commonAttr: {url: imgUrl} });
      })
    }
    //更新关联背景图片组件属性
    if(bgRelateCompId){
      const bgRelateCompData = this.componentMap[bgRelateCompId];
      const bgUrl = bgRelateCompData.lists[bgIndexs[searchImgIndex]].url;
      this.store.commit("updateComponentAttr", { id: bgRelateCompId, commonAttr: {url: bgUrl} });
    }
    //更新通用动态图片
    if(interactionResultImgs){
      const shareData = { url: interactionResultImgs[searchImgIndex]};
      this.store.commit("updateShareData", shareData);
    }
    // 更新博文数据
    if(isHasOwnProperty(interactionData_, 'pushCompId') && interactionData_.pushCompId) {
      const pushCompId = interactionData_.pushCompId;
      const pushCompData = this.componentMap[pushCompId];
      pushCompData.commonAttr.pushIndex = searchImgIndex;
    }
  }

  changeRelateCompData(event, clickItem) {
    //920我的抗疫毕业证
    let itemIndex = this.commonAttr.itemIndex;
    const compIdStrs = event.comps[1].attrs.value.split(",");
    const compIds = [];
    let compId = '';
    _.forEach(compIdStrs, (compIdStr) => {
      compId = getCompIdByParam(compIdStr)
      if(compId){
        compIds.push(compId)
      }
    })
    let triggerRelateCompId = compIds[0];
    if(triggerRelateCompId){
      let triggerRelateCompData = this.componentMap[triggerRelateCompId];
      const triggerEvents = triggerRelateCompData.events;
      if(triggerEvents.user || triggerEvents.vote){
        //数据有可能是前端配置的uid，也有可能是后端用户数据id
        this.store.commit('updateComponentAttr', { id: triggerRelateCompId, commonAttr: { voteId: clickItem.uid || clickItem.id } });
      }else if(triggerEvents.rotate3d || triggerEvents.interactionData){
        let triggerRelateData;
        if(event.comps[1].attrs.compIdObj){
          triggerRelateData = JSON.parse(event.comps[1].attrs.compIdObj);
        }

        let url = '';
        let imgs = null;
        let item = null;
        _.forEach(compIds, (compId) => {
          url = '';
          item = null;
          imgs = null;
          triggerRelateCompId = compId;
          triggerRelateCompData = this.componentMap[triggerRelateCompId];
          if(triggerRelateCompData && triggerRelateCompData.cid == COMPONENT_TYPES.wb_img){
            if(triggerRelateCompData.lists && triggerRelateCompData.lists[itemIndex]){
              url = triggerRelateCompData.lists[itemIndex].url;
            }else if(triggerRelateCompData.interactionData){
              //支持在触发组件上存储数据（自定义数据事件）同时也支持在目标组件上存储数据（图片是字符串数组格式）
              if(triggerRelateCompData.interactionData.isDimension){
                if(triggerRelateCompData.interactionData){
                  imgs = triggerRelateCompData.interactionData.dimensionLists;
                }else{
                  if(triggerRelateData && triggerRelateData[compId]){
                    imgs = triggerRelateData[compId].imgs;
                  }
                }
                let itemIndex_ = 0;
                if(imgs){
                  //话题词 根据单选 映射后随机话题词 结果页 根据数据包来的数据和单选选中index来确定
                  if(triggerRelateData && triggerRelateData[compId] && Object.prototype.hasOwnProperty.call(triggerRelateData[compId], "dataAttr")){
                    //从后台数据中拿值来映射数据
                    let attr = triggerRelateData[compId]["dataAttr"];
                    const index = triggerRelateCompData.commonAttr[attr] ? triggerRelateCompData.commonAttr[attr] : 0;
                    item = imgs[index][itemIndex];
                    url = item.url || item;
                    itemIndex_ = index * imgs[index].length + itemIndex;
                  }else{
                    const randomIndex = _.random(0, imgs[itemIndex].length-1);
                    item = imgs[itemIndex][randomIndex];
                    url = item && item.url || item;
                    itemIndex_ = itemIndex * imgs[itemIndex].length + randomIndex;
                  }
                  triggerRelateCompData.commonAttr.itemIndex = itemIndex_;
                }
              }else{
                url = triggerRelateCompData.interactionData.lists[itemIndex].url;
              }
            }
            if(url){
              this.store.commit("updateComponentAttr", {id: triggerRelateCompId, commonAttr: {url}})
            }
          }
        })
      }
    }
  }
}
